package com.cykj.chat.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.cykj.chat.service.IChatService;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.ChatRecordDO;
import com.cykj.common.utils.SaTokenUtils;
import com.cykj.webSocket.AddWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.chat.controller
 * @Author: 蔡张君逸
 * @CreateTime: 2025-08-23 12:16
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("chat")
public class ChatController{
    @Autowired
    IChatService chatService;
  //获取生成左侧队列信息的列表
    @GetMapping("getMsgs")
    public ResponseDTO getAllMsgs(@RequestParam("myRole")String myRole,@RequestParam("myId")Integer myId,@RequestParam("role") String role){
        return chatService.getAllMsgs(myRole,myId,role);
    }
    //获取聊天记录，顺便已读
    @GetMapping("getConcretChat")
    public ResponseDTO getConcretChat(@RequestParam("myRole")String myRole,@RequestParam("myId")Integer myId,
                                      @RequestParam("yourRole")String yourRole,@RequestParam("yourRoleId")Integer yourRoleId){
        return chatService.getConcretChat(myRole,myId,yourRole,yourRoleId);
    }

    //写入聊天记录，修改自己联系人map的redis(若存在)，对方的联系人map的redis(若存在) 聊天记录的redis
    @GetMapping("write")
    public  ResponseDTO writeRecord(ChatRecordDO chatRecordDO){
        return chatService.writeRecord(chatRecordDO);
    }

    //制造新的聊天对象。只在自己的联系人列表添加新的一个人
    @GetMapping("talkToNewOne")
    public  ResponseDTO talkToNewOne(@RequestParam("myRole")String myRole,@RequestParam("myId")Integer myId,
                                     @RequestParam("yourRole")String yourRole,@RequestParam("yourRoleId")Integer yourRoleId){
        return chatService.talkToNewOne(myRole,myId,yourRole,yourRoleId);
    }

    //获取常用语
    @GetMapping("getCommonExpressions")
    public ResponseDTO getCommonExpression(@RequestParam("role") String role){
        return chatService.getCommonExpression(role);
    }

    //草稿修改，修改本地联系人列表，如果有非空草稿，会影响排序
    @GetMapping("updateDraft")
    public ResponseDTO updateDraft(ChatRecordDO chatRecordDO){
        return chatService.updateDraft(chatRecordDO.getSendRole(),chatRecordDO.getSendId()
                ,chatRecordDO.getReceiveRole(),chatRecordDO.getReceiveId(),chatRecordDO.getChatInfo());
    }
    //草稿
    @GetMapping("getDraft")
    public ResponseDTO getDraft(@RequestParam("myRole")String myRole,@RequestParam("myId")Integer myId,
                                @RequestParam("yourRole")String yourRole,@RequestParam("yourRoleId")Integer yourRoleId){
        return chatService.getDraft(myRole,myId,yourRole,yourRoleId);
    }

//    获取一个随机的在线客服id
    @GetMapping("getOnlineAdminId")
    public ResponseDTO getOnlineAdminId(){
        return ResponseDTO.success(AddWebSocketHandler.getOnlineAdmin());
    }



}